package Servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.DanhMucDAO;
import POJO.DanhMuc;

/**
 * Servlet implementation class ModifyDanhMucSanPhamServlet
 */
//@WebServlet("/ModifyDanhMucSanPhamServlet")
public class ModifyDanhMucSanPhamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyDanhMucSanPhamServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    String maDanhMuc = request.getParameter("maDanhMuc");
        DanhMuc dm = DanhMucDAO.SelectDanhMucById(maDanhMuc);
        
        request.setAttribute("danhMuc", dm);        
        
        RequestDispatcher dispatcher = request.getRequestDispatcher("/Modify/ModifyDanhMucSanPham.jsp");
        dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    DanhMuc danhMuc = new DanhMuc();
        if (request.getParameterValues("suaDanhMuc") != null)
        {
            String[] strs = request.getParameterValues("suaDanhMuc");

            // Chuyển sang chữ có dấu
            strs[0] = new String(strs[0].getBytes("ISO-8859-1"), "UTF-8");
            // Lấy tên
            danhMuc.setTenDanhMuc(strs[0]);

            // Lấy ngày tháng năm
            String date = strs[2] + "/" + strs[1] + "/" + strs[3];
            danhMuc.setNgayTao(date);

            // Lấy mô tả
            strs[4] = new String(strs[4].getBytes("ISO-8859-1"), "UTF-8");
            danhMuc.setMoTa(strs[4]);

            // Lấy số sản phẩm đang bán
            danhMuc.setSoSanPhamDangBan(Integer.parseInt(strs[5]));

            // Lấy tình trạng
            strs[6] = new String(strs[6].getBytes("ISO-8859-1"), "UTF-8");
            danhMuc.setTinhTrang(strs[6]);

            //Lấy mã danh mục
            danhMuc.setMaDanhMuc(Integer.parseInt(strs[7]));
            
            // cập nhật danh mục
            if (DanhMucDAO.UpdateDanhMuc(danhMuc))
            {
                System.out.println("AAA");
            }

            request.setAttribute("id", "1");
            ArrayList<DanhMuc> list = DanhMucDAO.SelectDanhMucALL();

            request.setAttribute("listDanhMuc", list);
            RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
            dispatcher.forward(request, response);
        }
	}

}
